// pages/personal/myPromote/qrcode/qrcode.js
var app = getApp()
var api = require('../../../../utils/api')
var apis = api.api
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		tx: 'https://cdn.oss.dgddaijia.com/icons/personal.png',
		isUser: '',
		accToken: null,
		qrcode: null,
		type: null, //用来判断是什么权限的用户进来的
		userInfo: null, //用户信息
		canvWidth: null,
		canvHeight: null,
	},

	getData() {
		// https: //dageda.api.hjdz.ltd/sys/getToken
		var that = this;
		var url = apis.host + "index/qrcode";
		var user = wx.getStorageSync('userAllInfo');
		wx.showLoading({
			title: '加载中'
		})
		var tgData = {
			id: user.id, //用户的id
			type: that.data.type, //用户的权限
			recomment: user.recomment,
			trader: user.trader ? user.trader.id : 0
		}
		var urlData = {
			content: JSON.stringify(tgData),
			/* 需要传递的内容 */
			env: 'develop',
			/* 小程序版本 */
			// 要打开的小程序版本。正式版为 "release"，体验版为 "trial"，开发版为 "develop"
		};
		if (this.data.type === 'usermore12') {
			urlData.path = "pages/personal/applyMerchants/applyMerchants"
		} else if (this.data.type === 'usermore1') {
			urlData.path = "pages/login/login"
		} else if (this.data.type === 'traderorder') {
			urlData.path = "pages/index/index"
		} else if (this.data.type === 'getorder') {
			urlData.path = "pages/index/index"
		}
		var data = {
			type: this.data.type,
			/* 是	string	二维码类型 djmore 司机推广 usermore 用户推广 */
			url: JSON.stringify(urlData),
			/* 是	string	扫描二维码后跳转的链接或是字符串 */
			qrcode: 'lite' /* 是	string	生成码类型 lite 小程序码 text 普通二维码 默认text */
		}
		apis.request(url, data, 'POST').then(function(res) {
			if (res.code == 200) {
				that.setData({
					qrcode: res.data.path
				})
				wx.hideLoading()
			}
		})
	},
	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function(options) {
		this.setData({
			type: options.type
		})
		// console.log('type', options.type)
	},
	save() {
		// 保存二维码信息
		var that = this;
		wx.showLoading({
			title: '生成图片...',
		})
		var canvas = wx.createCanvasContext('share');
		var box = wx.createSelectorQuery();
		box.select('.qrcode_cont').boundingClientRect(function(rect) {
			that.setData({
				canvWidth: rect.width,
				canvHeight: rect.height,
			})
			canvas.fillStyle = "#fff";
			canvas.fillRect(0, 0, rect.width, rect.height)
			canvas.setFillStyle = "#000";
			wx.getImageInfo({
				src: that.data.userInfo.avatar,
				success(imgres2) {
					let wid1 = (rect.width) / 4,
						hei1 = (rect.height) / 4,
						top = 30,
						left = 30;
					canvas.drawImage(imgres2.path, 0, 0, rect.width, rect.height, left,
						top, wid1, hei1); //绘制图片到画布
					canvas.setFontSize(20)
					canvas.fillText(that.data.userInfo.username, wid1 + 50, hei1 + 10)
					wx.getImageInfo({
						src: that.data.qrcode,
						success(imgres) {
							canvas.drawImage(imgres.path, 0, 0, (rect.width * 1.5), (
								rect.width * 1.5), (rect.width - ((rect.width *
								0.8))) / 2, (rect.height - ((rect.width *
								0.8))) - 20, (rect.width * 0.8), (rect.width *
								0.8)); //绘制图片到画布
							canvas.draw(true, function(res2) {
								wx.canvasToTempFilePath({
									canvasId: 'share',
									width: rect.width,
									height: rect.height,
									success(res) {
										wx.hideLoading()
										wx.saveImageToPhotosAlbum({
											filePath: res
												.tempFilePath,
											success(res) {
												wx.showModal({
													title: '保存成功',
													content: "请在相册中查看"
												})
											},
											fail(res) {
												if (res
													.errMsg ===
													'saveImageToPhotosAlbum:fail auth deny'
												) {
													//申请权限  scope.writePhotosAlbum
													wx.authorize({
														scope: "scope.writePhotosAlbum",
														success(
															res
														) {
															wx.saveImageToPhotosAlbum({
																filePath: res
																	.tempFilePath,
																success(
																	res
																) {
																	wx.showModal({
																		title: '保存成功',
																		content: "请在相册中查看"
																	})
																},
																fail(
																	res
																) {
																	wx.showModal({
																		title: '保存失败',
																		content: "请检查您的权限设置"
																	})
																}
															})
														},
														fail(
															res
														) {
															wx.showModal({
																title: '提示',
																content: "您未授权，无法保存，请重新进入小程序"
															})
														}
													})
												}
											}
										})
									},
									fail(res) {
										wx.hideLoading()
										wx.showToast({
											title: '生成失败',
											icon: 'error'
										})
									}
								})
							})
						},
						fail(res) {
							wx.hideLoading()
							wx.showToast({
								title: '网络图片加载失败',
								icon: 'error'
							})
						}
					})
				},
				fail(res) {
					wx.hideLoading()
					wx.showToast({
						title: '图片加载失败',
						icon: 'error'
					})
				}
			})
		}).exec();

	},
	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function() {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function() {
		// this.getData()
		var that = this
		wx.showLoading({
			title: '加载中'
		})
		that.setData({
			userInfo: wx.getStorageSync('userAllInfo')
		})
		var isQrcode = wx.getStorageSync('userAllInfo').qrcode
		// console.log(isQrcode)
		if (isQrcode.length == 0 || isQrcode[this.data.type] === undefined) {
			that.getData()
		} else {
			that.setData({
				qrcode: wx.getStorageSync('userAllInfo').qrcode[this.data.type]
			})
			wx.hideLoading()
		}
	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function() {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function() {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function() {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function() {

	}
})
